<?php
namespace App\Services\Admin;

use Illuminate\Support\Facades\DB;

class ShopService extends BaseService
{
    /**
     * 场景
     */

    /**
     * 获取列表
     * @param array $pages【start起始页，limit每页记录数】
     * return array
     */
    public function getIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbShop)->where('del', 1);
        $total= $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有了');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'shop_name' => $v['shop_name'],
                'shop_code' => $v['shop_code'],
                'headimg' => $v['headimg'],
                'code' => $v['qrcode'],
                'uptime' => $v['uptime'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    /**
     * 获取列表
     * @param int $id
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbShop)
                    ->where('id', $id)
                    ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        return $this->toback(0, '', $res);
    }

    /**
     * 添加门店
     * @param array $pas
     * return array
     */
    public function add($pas)
    {
        $pas['shop_code'] = toCode();
        $pas['addtime'] = $pas['uptime'] = toDate();
        $shopRst = $this->dbConn($this->dbShop)->insertGetId($pas);
        if (!$shopRst) {
            return $this->toback(-1, 'shop error');
        }
        return $this->toback();
//        $userShop = [
//            'phone' => $pas['phone'],
//            'shop_code' => $shopCode,
//            'is_leader' => 3,
//            'addtime' => toDate(),
//            'uptime' => toDate(),
//        ];
//        DB::beginTransaction(); //开启事务
//        try {
//            //门店数据
//            $shopRst = $this->dbConn($this->dbShop)->insertGetId($pas);
//            if (!$shopRst) {
//                return $this->toback(-1, 'shop error');
//            }
//            //关联数据
//            $usRst = $this->dbConn($this->dbUs)->insertGetId($userShop);
//            if (!$usRst) {
//                return $this->toback(-1, 'us error');
//            }
//            DB::commit(); //提交事务
//            return $this->toback();
//        } catch (\Exception $e) {
//            DB::rollBack(); //事务回滚
//            echo $e->getMessage();
//            return $this->toback(-1, 'error');
//        }
    }

    /**
     * 修改
     * @param array $pas
     * return array
     */
    public function modify($id, $pas)
    {
        $shopOne = (array) $this->dbConn($this->dbShop)
            ->where('id', $id)
            ->first();
        if (empty($shopOne)) {
            $this->toback(-2, '门店不存在');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbShop)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
//        $userShop = [
//            'phone' => $pas['phone'],
//            'uptime' => toDate(),
//        ];
//        DB::beginTransaction(); //开启事务
//        try {
//            //门店数据
//            $shopRst = $this->dbConn($this->dbShop)
//                ->where('shop_code', $shopCode)
//                ->update($shopArys);
//            if (!$shopRst) {
//                return array('code' => -2, 'msg' => 'shop fail');
//            }
//            //老板数据
//            if ($shopOne['phone'] != $params['phone']) {
//                $userRst = $this->dbConn($this->dbUs)
//                    ->where('shop_code', $shopCode)
//                    ->where('phone', $shopOne['phone'])
//                    ->update($userShop);
//                if (!$userRst) {
//                    return array('code' => -2, 'msg' => 'boss fail');
//                }
//            }
//            DB::commit(); //提交事务
//            return array('code' => 0, 'msg' => 'success', 'data'=>$shopRst);
//        } catch (\Exception $e) {
//            DB::rollBack(); //事务回滚
//            echo $e->getMessage();
//            return array('code' => -1, 'msg' => 'error');
//        }
    }

    /**
     * 修改
     * @param int $id
     * return array
     */
    public function del($id)
    {
        $shopOne = (array) $this->dbConn($this->dbShop)
            ->where('id', $id)
            ->first();
        if (empty($shopOne)) {
            $this->toback(-2, '门店不存在');
        }
        $up = $this->dbConn($this->dbShop)->where('id', $id)
            ->update(['del' => 2, 'deltime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }
}